/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.tool.string;

import android.text.TextUtils;

import androidx.annotation.Nullable;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Young Ken
 */

public class StringUtil {
    /**
     * is value is empty return true
     *
     * @param value value
     * @return empty return true
     */
    public static boolean isEmpty(String value) {

        return null == value || value.isEmpty();
    }

    /**
     * 判断是否为空
     *
     * @param strings 需要判空的字符串集
     * @return 是否为空.
     */
    public static boolean isEmpty(String... strings) {

        for (String str : strings) {
            if (isEmpty(str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 字符为null返回""
     * 不为空，返回原字段.
     *
     * @param string the value
     * @return ""
     */
    public static String nullToEmpty(@Nullable String string) {
        return string == null ? "" : string;
    }

    /**
     * 判断是否包含表情
     *
     * @param source 字符串
     * @return 是否包含表情.
     */
    public static boolean containsEmoji(String source) {
        int len = source.length();
        boolean isEmoji = false;
        for (int i = 0; i < len; i++) {
            char hs = source.charAt(i);
            if (0xd800 <= hs && hs <= 0xdbff) {
                if (source.length() > 1) {
                    char ls = source.charAt(i + 1);
                    int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                    if (0x1d000 <= uc && uc <= 0x1f77f) {
                        return true;
                    }
                }
            } else {
                // non surrogate
                if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
                    return true;
                } else if (0x2B05 <= hs && hs <= 0x2b07) {
                    return true;
                } else if (0x2934 <= hs && hs <= 0x2935) {
                    return true;
                } else if (0x3297 <= hs && hs <= 0x3299) {
                    return true;
                } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d
                        || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c
                        || hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
                    return true;
                }
                if (!isEmoji && source.length() > 1 && i < source.length() - 1) {
                    char ls = source.charAt(i + 1);
                    if (ls == 0x20e3) {
                        return true;
                    }
                }
            }
        }
        return isEmoji;
    }

    /**
     * 检查密码是否同时包含数字，字母，切长度为6到16
     *
     * @param value String
     * @return boolean
     */
    public static boolean checkPassword(String value) {
        String all = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
        return Pattern.matches(all, value);
    }


    /**
     * 检查密码是否同时包含数字，字母，和符号，切长度在8到16之间
     *
     * @param value String
     * @return boolean
     */
    public static boolean checkComplexPassword(String value) {
        String all = "^(?![0-9]+$)(?![a-zA-Z_]+$)(?![\\W]+$)(?![\\w]+$)(?![0-9\\W]+$)(?![a-zA-Z_\\W]+$)[\\w\\W]{8,16}$";
        return Pattern.matches(all, value);
    }

    private static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(16[6])"
            + "|(17[0,1,3,5-8])|(18[0-9])|(19[8,9]))\\d{8}$";

    /**
     * 判别手机是否为正确手机号码
     * Regex of exact mobile.
     *
     * @param mobiles mobiles
     *                <p>china mobile: 134(0-8), 135, 136, 137, 138, 139, 147, 150, 151, 152, 157,
     *                158, 159, 178, 182, 183, 184, 187, 188, 198</p>
     *                <p>china unicom: 130, 131, 132, 145, 155, 156,
     *                166, 171, 175, 176, 185, 186</p>
     *                <p>china telecom: 133, 153, 173, 177, 180, 181, 189, 199</p>
     *                <p>global star: 1349</p>
     *                <p>virtual operator: 170</p>
     * @return boolean
     */
    public static boolean isMobileNum(String mobiles) {
        if (TextUtils.isEmpty(mobiles)) {
            return false;
        }
        Pattern pattern = Pattern.compile(REGEX_MOBILE_EXACT);
        Matcher matcher = pattern.matcher(mobiles);
        return matcher.matches();
    }
}
